import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author ml
 * @create 2017-07-28--13:36
 */
public class XmlUtil {

    /**
     * 解析XML文档
     * @param xmlFile
     * @return XML文档
     * @throws DocumentException
     * @throws FileNotFoundException
     */
    public static Document parse(String xmlFile) throws DocumentException, FileNotFoundException {
        SAXReader saxReader=new SAXReader();
        Document doc=saxReader.read(new File(xmlFile));
        return doc;
    }

    /**
     * 保存XML文档
     * @param doc
     * @throws IOException
     */
    public static void saveDocument(Document doc,String path) throws IOException {
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(new FileOutputStream(path),format);
        writer.write(doc);
        writer.close();
    }
}
